#include "display_bridge.h"
#include "stdio.h"
#include "lvgl.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "env.h"
#include "ui.h"
#include <time.h>
#include "esp_log.h"
#include "esp_err.h"
#include "esp_check.h"
#include "sensor_gpio.h"
#include "driver/i2c_master.h"

#define ESP_PLATFORM 1 // ESP32平台

#include "SensorPCF8563.hpp"

static const char *TAG = "DISPLAY_BRIDGE";

/**
 * 初始化ic2总线
 */
static i2c_master_bus_handle_t i2c_bus_handle;
static void init_i2c_bus()
{
    i2c_master_bus_config_t i2c_bus_config = {
        .i2c_port = SENSOR_I2C_BUS,
        .sda_io_num = SENSOR_I2C_GPIO_SDA,
        .scl_io_num = SENSOR_I2C_GPIO_SCL,
        .clk_source = I2C_CLK_SRC_DEFAULT,
        .glitch_ignore_cnt = 7,
        .intr_priority = 0,
        .flags = {
            .enable_internal_pullup = true,
        }
    };

    ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &i2c_bus_handle));
}

/**
 *  时间更新任务
 */
//设置系统时间
static void set_internal_rtc_time(struct tm* timeinfo) {
    // 定义要设置的时间（2023-11-20 15:30:00）
    // struct tm timeinfo = {
    //     .tm_year = 123,  // 2023 - 1900
    //     .tm_mon = 10,    // 11月（0-based）
    //     .tm_mday = 20,
    //     .tm_hour = 15,
    //     .tm_min = 30,
    //     .tm_sec = 0
    // };

    // 转换为 time_t 并设置系统时间
    time_t t = mktime(timeinfo);
    struct timeval now = { .tv_sec = t };
    settimeofday(&now, NULL);

    ESP_LOGI("RTC", "Internal RTC set to: %s", asctime(timeinfo));
}
SensorPCF8563 rtc;
static void update_clock_task(void *arg)
{
    lv_obj_t *clock_page = ui_home;
    lv_obj_t *seconds_pointer = ui_second;
    lv_obj_t *minutes_pointer = ui_minute;
    lv_obj_t *hours_pointer = ui_hour;

    static time_t current_time;
    static struct tm timeinfo_storage;
    static struct tm *timeinfo = &timeinfo_storage;

    // static bool rtc_inited = false;

    if (rtc.begin(i2c_bus_handle, PCF8563_SLAVE_ADDRESS))
    {
        ESP_LOGI(TAG, "rtc begin success");
        // rtc_inited = true;
        rtc.getDateTime(timeinfo);
        
        ESP_LOGI(TAG, "rtc current time %d-%d-%d %d:%d:%d", timeinfo->tm_year + 1900, timeinfo->tm_mon + 1, timeinfo->tm_mday, timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec);
        set_internal_rtc_time(timeinfo);
    }

    while (1)
    {
        vTaskDelay(pdMS_TO_TICKS(1000));
        time(&current_time);
        char *timestr = ctime(&current_time);
        ESP_LOGI(TAG, "now time %s", timestr);
        timeinfo = localtime(&current_time);
        int tm_sec = timeinfo->tm_sec;
        int tm_min = timeinfo->tm_min;
        int tm_hour = timeinfo->tm_hour;
        // int tm_wday = timeinfo->tm_wday;
        // int tm_mday = timeinfo->tm_mday;
        // int tm_mon = timeinfo->tm_mon + 1;
        // int tm_year = timeinfo->tm_year + 1900;

        if (clock_page)
        { // 判断是否存在时钟页面
            lv_img_set_angle(seconds_pointer, tm_sec * 10 * 6);
            // 每分钟6°
            lv_img_set_angle(minutes_pointer, tm_min * 10 * 6 + tm_sec * 10 * 6 / 60);
            // 每小时30°
            lv_img_set_angle(hours_pointer, tm_hour * 10 * 30 + tm_min * 10 * 30 / 60);
        }
    }
    vTaskDelete(NULL);
}

static void update_clock()
{
    xTaskCreate(update_clock_task, "update_clock_task", 1024 * 8, NULL, 1, NULL);
}

/**
 * 时间更新任务 end ******************
 */

void listen_display_event()
{
    ESP_LOGI(TAG, "listen_display_event");

#if DEVELOPMENT // 开发模式

#endif

#if PRODUCTION // 生产模式

#endif

    init_i2c_bus();
    // 时间更新
    update_clock();

    // TODO: 监听lvgl事件
}



// void light_on_screen()//亮屏
// {

// }
// void light_off_screen()//灭屏
// {

// }


// void sr_start_listen()//语音识别
// {

// }
// void sr_stop_listen()
// {

// }


// void tts_chinese_say(const char* text)//说中文
// {

// }

// void record_sound()//录制pcm音频
// {

// }
// void play_sound(uint16_t* pcm)//播放pcm音频
// {

// }